package com.wyd.jwpkglxt_ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyd.jwpkglxt_ssm.entity.Department;
import com.wyd.jwpkglxt_ssm.dao.DepartmentDao;
import com.wyd.jwpkglxt_ssm.service.DepartmentService;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * (Department)表服务实现类
 *
 * @author wyd
 * @since 2021-08-03 11:37:19
 */
@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Department queryById(Integer id) {
        return this.departmentDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Department> queryAllByLimit(int offset, int limit) {
        return this.departmentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param department 实例对象
     * @return 实例对象
     */
    @Override
    public Department insert(Department department) {
        this.departmentDao.insert(department.setCreateTime(new Timestamp(new Date().getTime())));
        return department;
    }

    /**
     * 修改数据
     *
     * @param department 实例对象
     * @return 实例对象
     */
    @Override
    public Department update(Department department) {
        this.departmentDao.update(department.setUpdateTime(new Timestamp(new Date().getTime())));
        return this.queryById(department.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.departmentDao.deleteById(id) > 0;
    }

    @Override
    public PageInfo<Department> queryAllByPage(Integer pageNum, Integer pageSize, Department department) {
        PageHelper.startPage(pageNum,pageSize);
        List<Department> departments = this.departmentDao.queryAll(department);
        return new PageInfo<>(departments);
    }

    @Override
    public Boolean deleteDepartmentBatch(List<Integer> departmentIds) {
        return this.departmentDao.deleteDepartmentBatch(departmentIds) > 0;
    }

    @Override
    public List<Department> queryAll(Department department) {
        return this.departmentDao.queryAll(department);
    }
}